package fr.afcepf.ai90.venteenligne.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
 * l'interface javax.sql.DataSource est prédéfinie en java et correspond à
 * l'interface abstraite d'un composant qui sert à se connecter à la base
 * .getConnection() ....
 */
public class SimpleDataSource implements DataSource {

    @Override
    public Connection getConnection() throws SQLException {
        Connection cn = null;
        try {
            ResourceBundle ressources =
                    ResourceBundle.getBundle("db"); // db.properties
            // chargement du driver jdbc pour mysql:
            // Class.forName("com.mysql.jdbc.Driver");
            Class.forName(ressources.getString("jdbcDriver"));
            // connexion à la base de données:
            // String dbUrl = "jdbc:mysql://localhost/minibank_db_ex1";
            String dbUrl = ressources.getString("url");
            String username =
                    ressources.getString("username");
            String password =
                    ressources.getString("password");
            cn = DriverManager.getConnection(dbUrl, username, password);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return cn;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter arg0) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setLoginTimeout(int arg0) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isWrapperFor(Class<?> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public <T> T unwrap(Class<T> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Connection getConnection(String username, String password)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

}
